package nodes

import (
	. "haproxy-mgr-go/models"
	"github.com/astaxie/beego/orm"
	"fmt"
)

func FindAll() []Nodes {
	nodes := []Nodes{}
	orm.NewOrm().QueryTable("nodes").All(&nodes)

	return nodes
}

func AddNodes(node *Nodes) {
	if success,err := orm.NewOrm().Insert(node); err != nil {
		fmt.Println("insert fail.....")
	}else {
		fmt.Printf("success inserted %d datas", success)
	}
}

func DelNodes(node *Nodes) {
	if success,err := orm.NewOrm().Delete(node); err != nil {
		fmt.Println("delete fail.....")
	}else {
		fmt.Printf("success delete %d datas", success)
	}

}

func ReplaceAll(nodes []Nodes) {
	o := orm.NewOrm()
	if res, err := o.Raw("TRUNCATE TABLE nodes").Exec(); err!=nil {
		fmt.Println("truncate table error...")
	} else {
		num, _ := res.RowsAffected()
		fmt.Println("truncate table nodes row affected nums: ", num)

		if success,err := o.InsertMulti(len(nodes), nodes); err!=nil {
			fmt.Println("insert fail.....")
		} else {
			fmt.Printf("success inserted %d datas", success)
		}
	}
}